﻿using System;
using Microsoft.Xna.Framework;

namespace MyCraft.Minecraft_2D.Blocs
{
    class Grass : Bloc
    {
        #region Attributs
        #endregion

        #region Constructeur
        public Grass(double timer)
            : base(timer, 15)
        {
            _light = -6;
            _minLight = 0;
            _waitTime = new Random((int)timer).Next(16000, 20000);
            _id = 2;
            _name = "Grass";
            _percent = 50;
            _color = Color.Green;
        }
        #endregion

        #region Proprietes
        #endregion

        #region Methodes
        public override TypeBloc OnNew(Block bloc)
        {
            Random random = new Random();
            int rnd = random.Next(BlocSet.GetSingleton()[TypeBloc.Wood].Percent + BlocSet.GetSingleton()[TypeBloc.Air].Percent);

            if ((bloc.Top != null && bloc.Top != TypeBloc.Air) && !bloc.Top.IsSun)
                return BlocSet.GetSingleton()[TypeBloc.Dirt].OnNew(bloc);

            if (rnd < BlocSet.GetSingleton()[TypeBloc.Wood].Percent)
            {
                TypeBloc newbloc = BlocSet.GetSingleton()[TypeBloc.Wood].OnNew(bloc);
                if (newbloc != TypeBloc.None)
                    bloc.Top.Type = newbloc;
            }

            if (bloc.Bottom!=null &&  bloc.Bottom == TypeBloc.Grass)
            {
                TypeBloc newbloc = BlocSet.GetSingleton()[TypeBloc.Dirt].OnNew(bloc);
                if (newbloc != TypeBloc.None)
                    bloc.Bottom.Type = newbloc;
            }
            return TypeBloc.Grass;
        }
        public override Bloc Update(Block block, double timer)
        {
            if (timer > _startTimer + _waitTime)
            {
                if (block.Top != null)
                {
                    if (block.Top.Type != TypeBloc.Air)
                        return BlocSet.GetSingleton()._newBlocs[TypeBloc.Dirt].Invoke(new Vector2(0.5f), timer);
                }
                Refresh(timer, 16000, 20000);
            }
            return this;
        }
        #endregion
    }
}
